<?php

namespace app\shop\model\hire;


use app\common\model\hire\Hire as HireModel;


/**
 * 课程模型
 */
class Hire extends HireModel
{
    /**
     * 课程列表
     */
    public function getList($data)
    {
        $model = $this;
        if (isset($data['search']) && $data['search']) {
            $model = $model->where('title', 'like', '%' . $data['search'] . '%');
        }
        if (isset($data['category_id']) && $data['category_id']) {
            $arr = Category::getSubCategoryId($data['category_id']);
            $model = $model->where('category_id', 'IN', $arr);
        }
        if (isset($data['is_show']) && $data['is_show'] > -1) {
            $model = $model->where('is_show', '=', $data['is_show']);
        }
        $list = $model->with(['category','image'])
            ->where('is_delete', '=', 0)
            ->order([ 'sort' => 'asc', 'hire_id' => 'desc'])
            ->paginate($data);
        return $list;
    }

    /**
     * 添加课程
     */
    public function add(array $data)
    {
//        if (!isset($data['banner']) || empty($data['banner'])) {
//            $this->error = '请上传banner图片';
//            return false;
//        }
        $data['app_id'] = self::$app_id;
        $time=$data['sign_start_sime'];
        $data['sign_start_sime']=strtotime($time[0]);
        $data['sign_end_sime']=strtotime($time[1]);
        $data['image_id']=$data['image_id'];
        // 开启事务
        $this->startTrans();
        try {
            // 添加课程
            $this->save($data);
            // 课程banner图片
         //   $this->addBanner($data['banner']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 编辑商品
     */
    public function edit($data)
    {
        $time=$data['sign_start_sime'];
        $data['sign_start_sime']=strtotime($time[0]);
        $data['sign_end_sime']=strtotime($time[1]);

        $status = $this->transaction(function () use ($data) {
            // 保存课程

            $this->save($data);
            // 课程banner图片
           // $this->addBanner($data['banner']);
            return true;
        });
        return $status;
    }




    /**
     * 修改状态
     */
    public function setStatus($state)
    {
        return $this->save(['is_show' => $state]) !== false;
    }



    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 获取总数
     */
    public function getTotal($where = [])
    {
        return $this->where('is_delete', '=', 0)->where($where)->count();
    }

    /**
     * 获取总数
     */
    public function getCourseTotal($day)
    {
        $model = $this;
        if (!is_null($day)) {
            $startTime = strtotime($day);
            $model = $model->where('create_time', '>=', $startTime)
                ->where('create_time', '<', $startTime + 86400);
        }
        return $model->where('is_delete', '=', '0')->count();
    }



}
